package com.qilin.hscs.hm.mqtt.config;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by 10188 on 2018/3/20.
 */
@Component
public class MqttClientFactory {


    @Autowired
    private MqttConnectOptions options;


    public MqttClient getClient(String clientId, String host) {
        MqttClient mqttClient=null;
        try {
            mqttClient= new MqttClient(host, clientId, new MemoryPersistence());
            mqttClient.connect(options);
            return mqttClient;
        } catch (MqttException me) {
            System.out.println("reason "+me.getReasonCode());
            System.out.println("msg "+me.getMessage());
            System.out.println("loc "+me.getLocalizedMessage());
            System.out.println("cause "+me.getCause());
            System.out.println("excep "+me);
            me.printStackTrace();
        }
        return null;
    }

}
